package cn.aj.user.controller;

import cn.aj.user.dto.EmailRegisterDto;
import cn.aj.user.dto.PhoneRegisterDto;
import cn.aj.user.service.IUserService;
import cn.aj.user.domain.User;
import cn.aj.user.query.UserQuery;
import cn.aj.basic.utils.AjaxResultUtil;
import cn.aj.basic.utils.PageListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    public IUserService userService;


    /**
     * 保存和修改公用的
     * @param user  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResultUtil addOrUpdate(@RequestBody User user){
        try {
            if( user.getId()!=null)
                userService.update(user);
            else
                userService.add(user);
            return AjaxResultUtil.getAjaxResult().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultUtil.getAjaxResult().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResultUtil delete(@PathVariable("id") Long id){
        try {
            userService.delete(id);
            return AjaxResultUtil.getAjaxResult().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResultUtil.getAjaxResult().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public User get(@PathVariable("id")Long id)
    {
        return userService.getById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<User> list(){

        return userService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageListUtil<User> json(@RequestBody UserQuery query)
    {
        return userService.loadPage(query);
    }


    /**
    * @Title: json
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/11/24 0024 19:38
    * @Parameters: [query]
    * @Return cn.aj.basic.utils.PageListUtil<cn.aj.user.domain.User>
     *     手机注册
    */
    @PostMapping("/phone")
    public AjaxResultUtil phoneRegister(@RequestBody PhoneRegisterDto phoneRegisterDto) {
         userService.phoneRegister(phoneRegisterDto);
         return AjaxResultUtil.getAjaxResult().success();
    }

    /**
    * @Title: emailRegister
    * @Description: TODO
    * @Author: Mr.GT
    * @Version: 1.0
    * @Date:  2022/11/25 0025 10:54
    * @Parameters: [phoneRegisterDto]
    * @Return cn.aj.basic.utils.AjaxResultUtil
     * 邮箱注册
    */
    @PostMapping("/email")
    public AjaxResultUtil emailRegister(@RequestBody EmailRegisterDto emailRegisterDto) {
        userService.emailRegister(emailRegisterDto);
        return AjaxResultUtil.getAjaxResult().success();
    }

}
